define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
	// 还原地址
	let restore_url = '';
	// 销毁地址
	let destroy_url = '';
	// 当前表格
	let currentTable = null;

	// 请求方法
	function request(url, msg) {
		if (!currentTable) return;
		const ids = Table.api.selectedids(currentTable);

		layer.confirm(
			msg,
			{
				title: '提示',
				btn: ['确定', '取消'],
			},
			(index) => {
				Backend.api.ajax(
					{
						url: url + '?ids=' + ids,
					},
					() => {
						//关闭弹框
						layer.close(index);

						//刷新表格
						currentTable.bootstrapTable('refresh');
					},
					(err, res) => {
						console.log(err, res);
						layer.close(index);
					}
				);
			}
		);
	}

	var Controller = {
		index: function () {
			// 初始化表格参数配置
			Table.api.init();

			// 标签页切换事件
			$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
				var panel = $($(this).attr('href'));
				if (panel.length > 0) {
					Controller.table[panel.attr('id')].call(this);
					$(this).on('click', function (e) {
						$($(this).attr('href')).find('.btn-refresh').trigger('click');
					});
				}
				//移除绑定的事件
				$(this).unbind('shown.bs.tab');
			});

			//必须默认触发shown.bs.tab事件
			$('ul.nav-tabs li.active a[data-toggle="tab"]').trigger('shown.bs.tab');

			// 点击还原回调
			$('.btn-restore').click(() => request(restore_url, '确定恢复吗？'));
			
			// 点击还原回调
			$('.btn-destroy').click(()=>request(destroy_url, '确定销毁吗？'));
		},
		table: {
			// 课程回收站
			subject: function () {
				// 表格1
				const table1 = $('#table1');

				// 设置请求地址
				restore_url = 'subject/recyclebin/subjectRestore';
				destroy_url = 'subject/recyclebin/subjectDestroy';
				// 设置当前表格
				currentTable = table1;

				table1.bootstrapTable({
					url: 'subject/recyclebin/subject',
					toolbar: '#toolbar1',
					pk: 'id',
					// 排序字段
					sortName: 'deletetime',
					// 默认排序方式
					sortOrder: 'desc',
					showExport: false,
					columns: [
						[
							{ checkbox: true },
							{ field: 'id', title: 'ID' },
							{ field: 'title', title: __('Titles'), operate: 'LIKE' },
							{ field: 'category.name', title: __('Cateid') },
							{ field: 'thumbs_text', title: __('Thumbs'), formatter: Table.api.formatter.image },
							{ field: 'price', title: __('Price'), sortable: true },
							{ field: 'likes_text', title: __('Likes') },
							{
								field: 'createtime',
								title: __('CreateTime'),
								sortable: true,
								operate: 'RANGE',
								addclass: 'datetimerange',
								formatter: Table.api.formatter.datetime,
							},
							{
								field: 'deletetime',
								title: __('DeleteTime'),
								sortable: true,
								operate: 'RANGE',
								addclass: 'datetimerange',
								formatter: Table.api.formatter.datetime,
							},
							{
								field: 'operate',
								title: __('Operate'),
								table: table1,
								events: Table.api.events.operate,
								buttons: [
									{
										name: 'restore',
										title: '恢复',
										classname: 'btn btn-xs btn-success btn-ajax',
										extend: "data-toggle='tooltip'",
										icon: 'fa fa-reply',
										confirm: '确认恢复？',
										url: 'subject/recyclebin/subjectRestore',
										success(data, ret) {
											Layer.alert(ret.msg);
											// 刷新表格
											table1.bootstrapTable('refresh');
										},
										error(data, ret) {
											console.log(data, ret);
											Layer.alert(ret.msg);
											return false;
										},
									},
									{
										name: 'destroy',
										title: '销毁',
										classname: 'btn btn-xs btn-danger btn-ajax',
										extend: "data-toggle='tooltip'",
										icon: 'fa fa-trash',
										confirm: '确认销毁？',
										url: 'subject/recyclebin/subjectDestroy',
										success() {
											// 刷新表格
											table1.bootstrapTable('refresh');
										},
										error(data, ret) {
											console.log(data, ret);
											Layer.alert(ret.msg);
											return false;
										},
									},
								],
								formatter: Table.api.formatter.operate,
							},
						],
					],
				});

				// 为表格1绑定事件
				Table.api.bindevent(table1);
			},
			// 课程订单回收站
			order: function () {
				// 表格2
				const table2 = $('#table2');

				// 设置请求地址
				restore_url = 'subject/recyclebin/orderRestore';
				destroy_url = 'subject/recyclebin/orderDestroy';
				// 设置当前表格
				currentTable = table2;

				table2.bootstrapTable({
					url: 'subject/recyclebin/order',

					toolbar: '#toolbar2',
					// 排序字段
					sortName: 'deletetime',
					// 默认排序方式
					sortOrder: 'desc',
					showExport: false,
					columns: [
						[
							{ checkbox: true },
							{ field: 'id', title: 'ID', sortable: true },
							{ field: 'total', title: __('Total'), sortable: true, formatter: (v) => '￥' + v },
							{ field: 'code', title: __('Code') },
							{
								field: 'createtime',
								title: __('CreateTime'),
								sortable: true,
								formatter: Table.api.formatter.datetime,
							},
							{
								field: 'deletetime',
								title: __('DeleteTime'),
								sortable: true,
								operate: 'RANGE',
								addclass: 'datetimerange',
								formatter: Table.api.formatter.datetime,
							},
							{
								field: 'operate',
								title: __('Operate'),
								table: table2,
								events: Table.api.events.operate,
								buttons: [
									{
										name: 'restore',
										title: '恢复',
										classname: 'btn btn-xs btn-success btn-ajax',
										extend: "data-toggle='tooltip'",
										icon: 'fa fa-reply',
										confirm: '确认恢复？',
										url: 'subject/recyclebin/orderRestore',
										success() {
											// 刷新表格
											table2.bootstrapTable('refresh');
										},
										error(data, ret) {
											console.log(data, ret);
											Layer.alert(ret.msg);
											return false;
										},
									},
									{
										name: 'destroy',
										title: '销毁',
										classname: 'btn btn-xs btn-danger btn-ajax',
										extend: "data-toggle='tooltip'",
										icon: 'fa fa-trash',
										confirm: '确认销毁？',
										url: 'subject/recyclebin/orderDestroy',
										success(data, ret) {
											Layer.alert(ret.msg);
											// 刷新表格
											table2.bootstrapTable('refresh');
										},
										error(data, ret) {
											console.log(data, ret);
											Layer.alert(ret.msg);
											return false;
										},
									},
								],
								formatter: Table.api.formatter.operate,
							},
						],
					],
				});

				// 为表格2绑定事件
				Table.api.bindevent(table2);
			},
		},
		api: {
			bindevent: function () {
				Form.api.bindevent($('form[role=form]'));
			},
		},
	};
	return Controller;
});
